package com.xing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.Filter.SensitivewordFilter;
import com.xing.common.lang.Result;
import com.xing.mapper.BlogMapper;
import com.xing.mapper.DiscussMapper;
import com.xing.model.Blog;
import com.xing.model.Discuss;
import com.xing.service.DiscussService;
import com.xing.util.JsonUtils;
import com.xing.util.KeyUtils;
import com.xing.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 柠檬有点甜
 * @since 2021-09-18
 */
@Service
public class DiscussServiceImpl extends ServiceImpl<DiscussMapper, Discuss> implements DiscussService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    DiscussMapper discussMapper;

    @Autowired
    BlogMapper blogMapper;

    @Override
    public Result selectDiscuss() {
//        固定查询10条数据
//        经常出现评论，那么把它保存到redis中，
        /*最新评论*/
        //判断里面有没有值，如果没有就添加值
        if (ObjectUtil.isEmpty(redisUtil.get(KeyUtils.MAP_KEY_NEW_DISCUSS))){
            // 给redis设置记录值
        List<Discuss> discusses = discussMapper.selectDiscuss();
        //先删除原来的key
        redisUtil.del(KeyUtils.MAP_KEY_NEW_DISCUSS);
        //用hash保存对象 key <id po>
        redisUtil.set(KeyUtils.MAP_KEY_NEW_DISCUSS, JsonUtils.objectToJson(discusses));
            return Result.succ(discusses);
        }else {
            /*从redis中获取list数据，进行反序列化*/
            List<Discuss> list = JsonUtils.jsonToList(String.valueOf(redisUtil.get(KeyUtils.MAP_KEY_NEW_DISCUSS)), Discuss.class);
            return Result.succ(list);
        }

    }

    @Override
    public Result selectDiscussById(int blogId,int pageid) {
        //pageid是第几页默认是1
        //pageint是从哪开始默认是0
        int pageint = (pageid-1)*5;
        List<Discuss> discussList = discussMapper.selectDiscussById(blogId,pageint);
        return Result.succ(discussList);
    }

    @Override
    @Transactional
    public Result updateDiscuss(Discuss discuss) {

        //敏感字过滤器
        SensitivewordFilter sensitivewordFilter = new SensitivewordFilter();
        String sting = discuss.getContent();
        if (sting!=null){
            String hou = sensitivewordFilter.replaceSensitiveWord(sting, 1, "*");
            //设置过滤后的内容
            discuss.setContent(hou);
            int insert = discussMapper.insert(discuss);
//            博客评论+1
            Blog blog = blogMapper.selectById(discuss.getBlogId());
            blog.setCommentCount(blog.getCommentCount()+1);
            blogMapper.updateById(blog);

        }

        return Result.succ(null);
    }

    @Override
    public void saveNewDiscuss() {
        /*插入时同步redis*/
        /*最新评论*/
        List<Discuss> discusses = discussMapper.selectDiscuss();
        //先删除原来的key
        redisUtil.del(KeyUtils.MAP_KEY_NEW_DISCUSS);
        //用hash保存对象 key <po>
        redisUtil.set(KeyUtils.MAP_KEY_NEW_DISCUSS, JsonUtils.objectToJson(discusses));
    }
}
